package worldutils.huntgame.Commands;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import worldutils.huntgame.AutoStart.Timer;
import worldutils.huntgame.HuntGame;
import worldutils.huntgame.Listener.AirDropSet;
import worldutils.huntgame.Utils.*;

public class mainCommand implements CommandExecutor {

    private final HuntGame plugin;

    public mainCommand(HuntGame plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        if (args.length == 0){
            sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-error-gethelp").replace("*c","/hg help"));
            return false;
        }

        if (args[0].equalsIgnoreCase("help")){
            if (sender instanceof Player){
                if (sender.isOp()){
                    sender.sendMessage(ChatColor.AQUA + "============================================");
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-join").replace("*h",HuntGame.getMessage("hunter")).replace("*r",HuntGame.getMessage("runner")).replace("*s",HuntGame.getMessage("spectator")));
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-info"));
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-chat"));
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-player"));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-start"));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-setspawn").replace("*h",HuntGame.getMessage("hunter")).replace("*erandom",HuntGame.getMessage("random")).replace("*r",HuntGame.getMessage("runner")));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-runtime"));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-stop"));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-reload"));
                    sender.sendMessage(ChatColor.AQUA + "============================================");
                } else {
                    sender.sendMessage(ChatColor.AQUA + "============================================");
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-join").replace("*h",HuntGame.getMessage("hunter")).replace("*r",HuntGame.getMessage("runner")).replace("*s",HuntGame.getMessage("spectator")));
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-info"));
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-chat"));
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-player"));
                    sender.sendMessage(ChatColor.AQUA + "============================================");
                }
            } else {
                Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "============================================");
                sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-start"));
                sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-stop"));
                sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-runtime"));
                sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-reload"));
                sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-player"));
                Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "============================================");
            }
            return false;
        } else if (args[0].equalsIgnoreCase("info")){
            sender.sendMessage(ChatColor.AQUA + "=============================================================================");
            sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-info"));
            sender.sendMessage(ChatColor.AQUA + "=============================================================================");
        } else if (args[0].equalsIgnoreCase("plugin")){
            sender.sendMessage(ChatColor.AQUA + "=====================");
            sender.sendMessage(ChatColor.AQUA + "插件: HuntGame");
            sender.sendMessage(ChatColor.AQUA + "作者:" + HuntGame.author);
            sender.sendMessage(ChatColor.AQUA + "版本:" + HuntGame.version);
            sender.sendMessage(ChatColor.AQUA + "=====================");
        } else if (args[0].equalsIgnoreCase("join") ){
            if (sender instanceof Player) {
                if (HuntGame.Gamestatus) {
                    if (TagUtils.getTag((Player) sender).equalsIgnoreCase(HuntGame.getMessage("spectator"))){
                        sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("game-start-command-join-spectator"));
                    } else {
                        sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("game-start-command-join"));
                    }
                } else {
                    if (args.length == 2) {
                        if (args[1].equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                            TagUtils.addTag((Player) sender, HuntGame.getMessage("hunter"));
                        }
                        if (args[1].equalsIgnoreCase(HuntGame.getMessage("runner"))) {
                            TagUtils.addTag((Player) sender, HuntGame.getMessage("runner"));
                        }
                        if (args[1].equalsIgnoreCase(HuntGame.getMessage("spectator"))) {
                            TagUtils.addTag((Player) sender, HuntGame.getMessage("spectator"));
                        }
                    } else {
                        sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-help-join").replace("*h",HuntGame.getMessage("hunter")).replace("*r",HuntGame.getMessage("runner")).replace("*s",HuntGame.getMessage("spectator")));
                    }
                    if (TagUtils.getTagCount(HuntGame.getMessage("runner")) != 0 && TagUtils.getTagCount(HuntGame.getMessage("hunter")) != 0){
                        if (Timer.getStats()){

                        } else {
                            Timer.starttick();
                        }
                    }
                }
            }
        } else if (args[0].equalsIgnoreCase("chat") ){
            if (args.length == 2){
                if (sender instanceof Player) {
                    PlayerUtils.sendMessageToTaggedPlayers(TagUtils.getTag((Player) sender), ChatColor.AQUA + TagUtils.getTag((Player) sender) + HuntGame.getMessage("team-chat") + sender.getName() + ": " + args[1]);
                }
            }
        } else if (args[0].equalsIgnoreCase("player") ){
            sender.sendMessage(ChatColor.AQUA + "==============");
            sender.sendMessage(ChatColor.YELLOW + "玩家：");
            sender.sendMessage(ChatColor.YELLOW + HuntGame.getMessage("hunter") + String.valueOf(TagUtils.getTagCount(HuntGame.getMessage("hunter"))));
            sender.sendMessage(ChatColor.YELLOW + HuntGame.getMessage("runner") + String.valueOf(TagUtils.getTagCount(HuntGame.getMessage("runner"))));
            sender.sendMessage(ChatColor.YELLOW + HuntGame.getMessage("spectator") + String.valueOf(TagUtils.getTagCount(HuntGame.getMessage("spectator"))));
            sender.sendMessage(ChatColor.AQUA + "==============");
        } else if (args[0].equalsIgnoreCase("setspawn") ){
            if (sender instanceof Player) {
                if (sender.isOp()) {
                    if (args.length == 2) {
                        if (args[1].equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                            HuntGame.ZhuishaSpawn = (Location) ((Player) sender).getLocation();
                            sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-setspawn").replace("*t",HuntGame.getMessage("hunter")));
                        } else if (args[1].equalsIgnoreCase("runner")) {
                            HuntGame.TaoshengSpawn = (Location) ((Player) sender).getLocation();
                            sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-setspawn").replace("*t",HuntGame.getMessage("runner")));
                        } else if (args[1].equalsIgnoreCase(HuntGame.getMessage("random"))) {
                            HuntGame.TaoshengSpawn = RandomSpawnUtils.getRandomLocation(((Player) sender).getWorld(), 0,0);
                            HuntGame.ZhuishaSpawn = RandomSpawnUtils.getRandomLocationTwo(HuntGame.TaoshengSpawn.getWorld(),HuntGame.TaoshengSpawn);
                            sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("command-setspawn").replace("*t",HuntGame.getMessage("random")));
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-setspawn").replace("*h",HuntGame.getMessage("hunter")).replace("*r",HuntGame.getMessage("runner")).replace("*random",HuntGame.getMessage("randomr")));
                    }
                } else {
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
                }
            }
        } else if (args[0].equalsIgnoreCase("start") ){
            if (sender.isOp()){
                if (TagUtils.getTagCount(HuntGame.getMessage("hunter")) != 0 && TagUtils.getTagCount(HuntGame.getMessage("runner")) != 0) {
                    if (HuntGame.TaoshengSpawn == null || HuntGame.ZhuishaSpawn == null) {
                        sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-setspawn").replace("*h",HuntGame.getMessage("hunter")).replace("*r",HuntGame.getMessage("runner")).replace("*random",HuntGame.getMessage("randomr")));
                    } else {
                        if (HuntGame.Gamestatus){
                            sender.sendMessage(HuntGame.getMessage("gamestart-stat-error"));
                        } else {
                            TimerUtils.startgame();
                        }
                    }
                } else {
                    sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("error-player-cant-start"));
                }
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        } else if (args[0].equalsIgnoreCase("compass") ){
            if (sender instanceof Player) {
                String info = TagUtils.getCloseRangePlayer(HuntGame.getMessage("runner"), (Player) sender);
                sender.sendMessage(info);
            }
        } else if (args[0].equalsIgnoreCase("runtime") ){
            if (sender.isOp()) {
                if (args.length == 2) {
                    if (!HuntGame.Gamestatus) {
                        HuntGame.runTime = Integer.parseInt(args[1]);
                        sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("success-seting"));
                    } else {
                        sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("fail-seting-game-start"));
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + HuntGame.getMessage("command-help-runtime"));
                }
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        } else if (args[0].equalsIgnoreCase("stop") ){
            if (sender.isOp()) {
                TimerUtils.stopgame(HuntGame.getMessage("admin-stop-game"),HuntGame.getMessage("draw"));
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        } else if (args[0].equalsIgnoreCase("reload") ){
            if (sender.isOp()) {
                plugin.reloadConfigFiles();
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("config-reload"));
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        } else if (args[0].equalsIgnoreCase("setairdrop") ){
            if (sender.isOp()) {

                if (sender instanceof Player) {
                    Inventory setAirDropmenu = AirDropSet.jsonToInventory(HuntGame.AirDropChestNR);
                    Player senderplayer = (Player) sender;
                    senderplayer.openInventory(setAirDropmenu);
                }
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        } else if (args[0].equalsIgnoreCase("summonairdrop") ){
            if (sender.isOp()) {

                if (sender instanceof Player) {
                    AirDropUtils.summonRandomAirDrop();
                }
            } else {
                sender.sendMessage(ChatColor.AQUA + HuntGame.getMessage("non-perm"));
            }
        }


        return false;
    }

}
